package Constroter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import operate.AttrOper;
import operate.DataOperate;
import operate.FormOperate;
import util.Page;
import util.StrUtil;

public class FormServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		String cid = request.getParameter("cid");
		if ("addFormAttr".equals(method)) {
			AttrOper.addAttr(request);
			request.getRequestDispatcher("/form/formAttr_list.jsp?id="+cid).forward(request, response);
		}
		
		if("addForm".equals(method)){
			FormOperate.addForm(request);
			request.getRequestDispatcher("/form/form_list.jsp").forward(request, response);
		}
		
		if("addData".equals(method)){
			DataOperate.addData(request);
		}
		if("updateData".equals(method)){
			DataOperate.updateData(request);
			request.getRequestDispatcher("/form/form_list.jsp").forward(request, response);
		}
	}
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	    	request.setCharacterEncoding("utf-8");
			String pageNum=request.getParameter("pageNum");
			String id=request.getParameter("id");
			Page page=new Page();
			if(StrUtil.exist(pageNum)){
				page.setPageNum(Integer.parseInt(pageNum));
			}
			page=DataOperate.findDataById(page, id);
			request.setAttribute("page", page);
			request.getRequestDispatcher("/form/data_list.jsp").forward(request, response);
	}
}
